package com.leetcode.q495;

/**
 * <p> description: desc</p>
 *
 * @author tanzc
 * @date 2022/3/30
 **/
public class FindPoisonedDuration {
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        int startTime = timeSeries[0], endTime = startTime + duration - 1;
        int total = 0;
        for (int i = 1; i < timeSeries.length; i++) {
            if (timeSeries[i] > endTime) {
                // 如果下次攻击在中毒时间外，则累加上一轮中毒时长
                total += endTime - startTime + 1;
                // 并重置本次中毒开始时间
                startTime = timeSeries[i];
            }
            // 这个中毒结束时间，发现本次攻击不管在没在中毒时间内，都要重算中毒结束时间
            endTime = timeSeries[i] + duration - 1;
        }
        // 最后一次攻击后的中毒时间累加
        total += endTime - startTime + 1;
        return total;
    }

    public int findPoisonedDuration2(int[] timeSeries, int duration) {
        int total = 0;
        int endTime = -1;
        for (int i = 0; i < timeSeries.length; i++) {
            if (timeSeries[i] <= endTime) {
                total += timeSeries[i] - timeSeries[i - 1];
            } else {
                total += duration;
            }
            endTime = timeSeries[i] + duration - 1;
        }
        return total;
    }
}
